<%-- 
    Document   : warn
    Created on : 2013-5-7, 11:42:30
    Author     : piaohao
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
    <head>
        <base href="<%= basePath%>"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@include file="../main/common.jsp" %>
        <title>预警设置</title>
        <script type="text/javascript">
            Ext.onReady(function() {
                var empStore = Ext.create('Ext.data.Store', {
                    fields: ["id", "name", "age", "sex", "id_card", "phone", "orgName"],
                    data: {
                        data: []
                    },
                    proxy: {
                        type: 'memory',
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                    },
                    autoLoad: true
                });
                var empGrid = Ext.create("Ext.grid.Panel", {
                    title: "通知职员列表",
                    columnLines: true,
                    frame: true,
                    selType: 'checkboxmodel', // 设置选择模式 
                    multiSelect: true, // 允许多选
                    forceFit: true, columns: [
                        {header: '主键', dataIndex: 'id', hidden: true},
                        {header: '姓名', dataIndex: 'name', width: 120},
                        {header: '年龄', dataIndex: 'age', width: 100},
                        {header: '性别', dataIndex: 'sex', width: 100},
                        {header: '身份证', dataIndex: 'id_card', width: 100},
                        {header: '电话', dataIndex: 'phone', width: 100},
                        {header: '所属机构', dataIndex: 'orgName', width: 100}
                    ],
                    store: empStore,
                    tbar: [
                        {
                            text: "移除",
                            iconCls: "remove",
                            handler: function() {
                                var sel = empGrid.getSelectionModel().getSelection();
                                empGrid.getStore().remove(sel);
                            }
                        }, "-", {
                            text: "移除全部",
                            iconCls: "remove",
                            handler: function() {
                                empGrid.getStore().removeAll();
                            }
                        }, "->", {
                            id: "empCombo",
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "安全职员",
                            displayField: 'name',
                            valueField: 'id',
                            editable: false,
                            emptyText: "--请选择--",
                            store: {
                                fields: ['name', 'id', "age", "sex", "id_card", "phone", "orgName"],
                                pageSize: 15,
                                proxy: {type: 'ajax',
                                    url: '',
                                    reader: {
                                        type: 'json',
                                        root: "data"
                                    }
                                }
                            },
                            listeners: {
                                select: function(combo, record) {
                                    var store = empGrid.getStore();
                                    if (store.indexOf(record[0]) === -1) {
                                        empGrid.getStore().add(record);
                                    }
                                    Ext.getCmp("empCombo").setValue("");
                                }
                            }
                        }
                    ]
                });
                var warnForm = Ext.create("Ext.form.Panel", {
                    url: "warn/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 1
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {
                            id: "orgCombo",
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "所属机构",
                            name: "warn.org_id",
                            displayField: 'name',
                            valueField: 'id',
                            emptyText: "--请选择--",
                            editable: false,
                            store: {
                                fields: ['name', 'id'],
                                proxy: {type: 'ajax',
                                    url: 'org/getAll',
                                    reader: {
                                        type: 'json'
                                    }
                                }
                            },
                            listeners: {
                                select: function(combo, record) {
                                    Ext.getCmp("empCombo").getStore().load({url: "emp/getByOrgId/" + record[0].data.id});
                                }
                            }
                        },
                        {
                            xtype: "combo",
                            fieldLabel: '预警规则', //子公司，分公司，控股公司，参股公司，其他
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "1、驾驶员年龄超过55周岁预警", value: 1},
                                    {text: "2、安全管理人员高中以下文化程度预警", value: 2}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "warn.type",
                            emptyText: "--请选择--",
                            value: "",
                            listeners: {
                                select: function(combo, record) {
                                    Ext.getCmp("content").setValue(record[0].get("text"));
                                }
                            }
                        },
                        {name: "warn.content", id: "content", fieldLabel: "预警内容", readOnly: true},
                        {
                            xtype: "combo",
                            fieldLabel: '状态',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "启用", value: "启用"},
                                    {text: "停用", value: "停用"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "warn.status",
                            emptyText: "--请选择--",
                            value: "启用"
                        },
                        {
                            name: "warn.scan_time",
                            fieldLabel: "扫描时间",
                            editable: false,
                            xtype: "timefield"
                        },
                        {name: "warn.emp_ids", xtype: "hidden"},
                        {name: "warn.id", xtype: "hiddenfield"}
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                var store = empGrid.getStore();
                                var empIds = [];
                                if (store.getCount() < 1) {
                                    Ext.Msg.alert('提示', "通知职员列表不能为空！");
                                    return;
                                }
                                for (i = 0; i < store.getCount(); i++) {
                                    empIds.push(store.getAt(i).get("id"));
                                }
                                warnForm.getForm().setValues({
                                    "warn.emp_ids": empIds
                                });
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                warnWindow.close();
                                                warnGrid.getStore().load();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                warnWindow.close();
                            }
                        }]
                });
                var warnWindow = Ext.create("Ext.window.Window", {
                    title: "预警规则",
                    maximized: true,
                    maximizable: true,
                    closeAction: "hide",
                    layout: "border",
                    modal: true,
                    items: [
                        {region: "west", layout: "fit", width: 300, items: [warnForm]},
                        {region: "center", layout: "fit", items: [empGrid]}
                    ]
                });
                var warnStore = Ext.create('Ext.data.Store', {
                    fields: ["id", "content", "orgName", "status", "scan_time"],
                    pageSize: 15,
                    proxy: {
                        type: 'ajax',
                        url: 'warn/list',
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                    },
                    autoLoad: true
                });
                var warnGrid = Ext.create("Ext.grid.Panel", {
                    title: "预警列表",
                    columnLines: true,
                    frame: true,
                    selType: 'checkboxmodel', // 设置选择模式 
                    multiSelect: true, // 允许多选
                    forceFit: true,
                    columns: [
                        {header: '主键', dataIndex: 'id', hidden: true},
                        {header: '预警规则', dataIndex: 'content', width: 120},
                        {header: '扫描时间', dataIndex: 'scan_time', width: 120},
                        {header: '状态', dataIndex: 'status', width: 120},
                        {header: '所属机构', dataIndex: 'orgName', width: 200}
                    ],
                    store: warnStore,
                    tbar: [
                        {xtype: "button", text: "查看", iconCls: "search",
                            handler: function() {
                                var sel = warnGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0].data;
                                var detailWindow = Ext.create("Ext.window.Window", {
                                    title: "预警信息",
                                    width: 800,
                                    height: 400,
                                    maximizable: true,
                                    modal: true,
                                    closeAction: "hide",
                                    layout: "fit",
                                    html: "<iframe id='detailFrame' src='warn/showDetail/" + record.id + "' width='100%' height='100%'></iframe>",
                                    buttons: [
                                        {text: "打印", iconCls: "print",
                                            handler: function() {
                                                Ext.get("detailFrame").dom.contentWindow.print();
                                            }},
                                        {text: "关闭", iconCls: "close",
                                            handler: function() {
                                                detailWindow.close();
                                            }}
                                    ]
                                });
                                detailWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "登记", iconCls: "add",
                            handler: function() {
                                warnForm.getForm().reset();
                                empStore.removeAll();
                                warnForm.getForm().url = "warn/add";
                                warnWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "变更", iconCls: "edit",
                            handler: function() {
                                var sel = warnGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0];
                                warnForm.getForm().url = "warn/update";
                                var tag = true;
                                Ext.Ajax.request({
                                    url: "warn/getWarnById",
                                    params: {
                                        warnId: record.get("id")
                                    },
                                    success: function(response) {
                                        var json = Ext.JSON.decode(response.responseText);
                                        warnForm.getForm().setValues({
                                            "warn.id": json.warn.id,
                                            "warn.org_id": json.warn.org_id,
                                            "warn.type": json.warn.type,
                                            "warn.content": json.warn.content,
                                            "warn.status": json.warn.status,
                                            "warn.scan_time": json.warn.scan_time
                                        });
                                        Ext.getCmp("orgCombo").getStore().load();
                                        empStore.removeAll();
                                        for (i = 0; i < json.emps.length; i++) {
                                            //["id", "name", "age", "sex", "id_card", "phone","orgName"]
                                            empStore.add({
                                                id: json.emps[i].id,
                                                name: json.emps[i].name,
                                                age: json.emps[i].age,
                                                sex: json.emps[i].sex,
                                                id_card: json.emps[i].id_card,
                                                phone: json.emps[i].phone,
                                                orgName: json.emps[i].orgName
                                            });
                                        }
                                    },
                                    failure: function(response) {
                                        var json = Ext.JSON.decode(response.responseText);
                                        Ext.Msg.alert("提示", json.msg, function() {
                                            tag = false;
                                        });
                                    }
                                });
                                if (!tag) {
                                    Ext.Msg.alert("提示", "获取预警信息失败！");
                                    return;
                                }
                                warnWindow.show();
                            }
                        },
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "删除", iconCls: "remove",
                            handler: function() {
                                Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                    if (id === "yes") {
                                        var mask = new Ext.LoadMask(warnGrid, {msg: "正在删除..."});
                                        mask.show();
                                        var sel = warnGrid.getSelectionModel().getSelection();
                                        var ids = [];
                                        for (i = 0; i < sel.length; i++) {
                                            ids.push(sel[i].data.id);
                                        }
                                        Ext.Ajax.request({
                                            url: "warn/delete",
                                            params: {
                                                ids: ids
                                            },
                                            success: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    warnGrid.getStore().remove(sel);
                                                    mask.hide();
                                                });
                                            },
                                            failure: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    mask.hide();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: warnStore,
                        displayInfo: true
                    })
                });
                var viewport = Ext.create("Ext.Viewport", {
                    layout: {type: "border"},
                    items: [
                        {region: "center", layout: "fit", items: [warnGrid]}
                    ]
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>
